<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:param name="tableName"/>
    <xsl:template match="/">
        <xsl:for-each select="CogDataSet/TableInfo[Table = $tableName]">
            <xsl:variable name="currentTable" select="Table"/>using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Data;
using <xsl:value-of select="Namespace"/>.Interface;

namespace <xsl:value-of select="Namespace"/>
{
    public partial class <xsl:value-of select="ClassPrefix"/>DataSet
    {
        public partial class <xsl:value-of select="ManagedTable"/>DataTable : IDataTable
        {
            public override void EndInit()
            {
                ColumnChanged += (sender, args) => ((<xsl:value-of select="ManagedTable"/>Row)args.Row).OnPropertyChanged(args);
            }
          <xsl:for-each select="ColumnInfo[IsPrimaryKey = 'true' or IsIndexed = 'true' or IsForeignKey = 'true']">
            public IEnumerable&lt;<xsl:value-of select="Table2"/>Row&gt; LoadBy<xsl:value-of select="Column"/>(<xsl:value-of select="ManagedDataType"/><xsl:text> </xsl:text><xsl:value-of select="ManagedColumn"/>)
            {
                return ExecuteQuery&lt;<xsl:value-of select="Table2"/>Row&gt;(this, "<xsl:value-of select="../Schema"/>.<xsl:value-of select="Table2"/>_LoadBy<xsl:value-of select="Column"/>",
                    new Dictionary&lt;string, object&gt; {{"@<xsl:value-of select="Column"/>", <xsl:value-of select="ManagedColumn"/>}});
            }
        </xsl:for-each>
            public void Save()
            {
                SaveChanges(this);
            }

            #region IDataTable Members

            public string SelectStoredProc
            {
                get { 
                      return "SELECT <xsl:for-each select="ColumnInfo[Table = $currentTable]">[<xsl:value-of select="Column"/>]<xsl:if test="not(position()=last())">," +
                             "</xsl:if></xsl:for-each>" +
                             "FROM <xsl:value-of select="Schema"/>.<xsl:value-of select="Table"/>_Select"; 
                    }
            }

            public string InsertStoredProc
            {
                get { return "<xsl:value-of select="Schema"/>.<xsl:value-of select="Table"/>_Insert"; }
            }

            public string UpdateStoredProc
            {
                get { return "<xsl:value-of select="Schema"/>.<xsl:value-of select="Table"/>_Update"; }
            }

            public string DeleteStoredProc
            {
                get { return "<xsl:value-of select="Schema"/>.<xsl:value-of select="Table"/>_Delete"; }
            }

            #endregion
        }
    <xsl:if test="/CogDataSet/TableInfo/ColumnInfo[ParentTable = $currentTable]">
        #region Child Tables

        partial class <xsl:value-of select="ManagedTable"/>Row : INotifyPropertyChanged
        {
            <xsl:for-each select="/CogDataSet/TableInfo/ColumnInfo[ParentTable = $currentTable]">
            <xsl:value-of select="Table2"/>DataTable _<xsl:value-of select="ManagedTable"/>By<xsl:value-of select="Column"/>;
            public <xsl:value-of select="Table2"/>DataTable <xsl:value-of select="Table2"/>By<xsl:value-of select="Column"/>
            {
                get
                {
                    if(_<xsl:value-of select="ManagedTable"/>By<xsl:value-of select="Column"/> == null)
                        _<xsl:value-of select="ManagedTable"/>By<xsl:value-of select="Column"/> = new <xsl:value-of select="Table2"/>DataTable();

                    if(_<xsl:value-of select="ManagedTable"/>By<xsl:value-of select="Column"/>.Cast&lt;<xsl:value-of select="Table2"/>Row&gt;().All(r => r.<xsl:value-of select="Column"/> != <xsl:value-of select="/CogDataSet/TableInfo/ColumnInfo[Table = $currentTable and IsPrimaryKey = 'true']/Column"/>))
                        _<xsl:value-of select="ManagedTable"/>By<xsl:value-of select="Column"/>.LoadBy<xsl:value-of select="Column"/>(<xsl:value-of select="/CogDataSet/TableInfo/ColumnInfo[Table = $currentTable and IsPrimaryKey = 'true']/Column"/>);

                    return _<xsl:value-of select="ManagedTable"/>By<xsl:value-of select="Column"/>;
                }
            }

            </xsl:for-each>
            public event PropertyChangedEventHandler PropertyChanged;
            public void OnPropertyChanged(DataColumnChangeEventArgs args)
            {
                var handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(args.Column.ColumnName));
            }
        }

        #endregion</xsl:if>
    <xsl:if test="not(/CogDataSet/TableInfo/ColumnInfo[ParentTable = $currentTable])">
        partial class <xsl:value-of select="ManagedTable"/>Row : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            public void OnPropertyChanged(DataColumnChangeEventArgs args)
            {
                var handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(args.Column.ColumnName));
            }
        }
    </xsl:if>
    }
}
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
